<?php
/**
 * Copyright (c) 2008, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2008, Julien PORTALIER
 * @link         http://code.google.com/p/feather-cm
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

/**
 * ...
 * 
 * All methods await the following data:
 * 
 * blog      (http://WC_DOMAIN/)
 * permalink (http://WC_DOMAIN/page/$urltitle)
 * comment_type (comment, trackback, pingback, registration, ...)
 * comment_author
 * comment_author_email
 * comment_author_url
 * comment_content
 * article_date ($Page->created)
 * 
 * user_ip
 * user_agent
 * referer
 */
class TypePadAntiSpamBehavior
{
	const protocol_version = '1.1';
	
	/**
	 * Checks if a comment is SPAM or HAM.
	 * 
	 * @return Boolean Returns true if comment is spam.
	 */
	function check_comment(array &$comment, $api_key=null)
	{
		if (empty($comment['referer'])) {
			$comment['referer'] = $_SERVER['HTTP_REFERER'];
		}
		if (empty($comment['user_ip'])) {
			$comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
		}
		if (empty($comment['user_agent'])) {
			$comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
		}
		
		list($headers, $body) = $this->http_post($comment, $api_key, 'comment-check');
		return ($body == 'true');
	}
	
	/**
	 * Checks if an api key is valid, or not.
	 * 
	 * @return Boolean
	 */
	function check_api_key($api_key, $host)
	{
		$request = array(
			'key'  => $api_key,
			'blog' => $host
		);
		list($headers, $body) = $this->http_post($request, false, 'verify-key');
		return ($body == 'valid');
	}
	
	/**
	 * Reports a comment as SPAM.
	 * 
	 * @param $comment Array
	 */
	function submit_spam(array &$comment, $api_key=null)
	{
		$this->http_post($comment, $api_key, 'submit-spam');
	}
	
	/**
	 * Reports a comment as HAM (thus, non SPAM).
	 * 
	 * @param $comment Array
	 */
	function submit_ham(array &$comment, $api_key=null)
	{
		$this->http_post($comment, $api_key, 'submit-ham');
	}
	
	/**
	 * Sends request
	 * 
	 * @return Array (headers, body)
	 * @param $request Array
	 * @param $api_key String
	 * @param $action String
	 * @param $port Integer[optional]
	 */
	protected function & http_post(array &$request, $api_key=null, $action, $port=80)
	{
		# webservice
		if ($action !== 'verify-key')
		{
			if (empty($api_key)) {
				$api_key = Core::typepad_antispam_api_key;
			}
			$host = $api_key.'.'.Core::typepad_antispam_host;
		}
		else {
			$host = Core::typepad_antispam_host;
		}
		$path = '/'.self::protocol_version.'/'.$action;
		
		# post data to ignore
		$ignore = array(
			'HTTP_COOKIE',
			'REDIRECT_URI',
			'SERVER_SOFTWARE',
			'PHP_FCGI_CHILDREN',
			'PHP_FCGI_MAX_REQUESTS',
			'FCGI_ROLE',
			'GATEWAY_INTERFACE',
			'PATH',
			'SCRIPT_NAME',
			'SCRIPT_FILENAME',
			'DOCUMENT_ROOT',
			'PHP_SELF',
		);
		foreach ($_SERVER as $key => $value)
		{
			if (!in_array($key, $ignore)) {
				$request["$key"] = $value;
			}
		}
		
		unset($request['argc']);
		unset($request['argv']);
#		debug($request);
#		exit;
		
		# HTTP REQUEST
		$query_string = '';
		foreach ($request as $k => $v) {
			$query_string .= $k.'='.urlencode(stripslashes($v)).'&';
		}
		$http_request  = "POST $path HTTP/1.0".CRLF;
		$http_request .= "Host: $host".CRLF;
		$http_request .= "Content-Type: application/x-www-form-urlencoded; charset=".Core::charset.CRLF;
		$http_request .= "Content-Length: ".strlen($query_string).CRLF;
		$http_request .= "User-Agent: FeatherPHP/2.0 | TypePadAntiSpam/0.1".CRLF;
		$http_request .= CRLF;
		$http_request .= $query_string;
		
		# REQUEST -> RESPONSE
		$response = '';
		if (false != ($fs = @fsockopen($host, $port, $errno, $errstr, 10)))
		{
			fwrite($fs, $http_request);
			while (!feof($fs))
			{
				# 1160 equals one TCP-IP packet
				$response .= fgets($fs, 1160);
			}
			fclose($fs);
			$response = explode(CRLF.CRLF, $response, 2);
		}
		
#		debug($response);
#		exit;
		
		return $response;
	}
}
?>
